<template>
	<view>
		<carHeader :isCar='isCar' @clear="clear"></carHeader>
		<view v-for="(item,index) in list" :key="index">
			<view class="shopbody" v-for="(sku,subIndex) in item.skus" :key="subIndex">
				<view class="gitem">
					<view class="left">
						<image class="img" referrerpolicy="no-referrer" :src="sku.image" />
					</view>
					<view class="right">
						<view class="name line-1">
							{{sku.productName}}
						</view>
						<view class="spac"></view>
						<view class="ops align-center">
							<view class="space-title">
								规格
							</view>
							<view class="space-content">
								{{sku.value}}
							</view>
						</view>
						<view class="price-count justify-between">
							<view class="price">
								<span class="unit">￥</span>
								<span class="price">{{sku.price}}</span>
							</view>
							<view class="mt10">
								<u-number-box button-size="29" v-model="sku.number" :min="0"
									@change="e =>countChange(e,sku.skuId)"></u-number-box>
							</view>
						</view>
					</view>
				</view>
				<u-line color="#eee"></u-line>
			</view>
		</view>
		<view class="total align-center">
			<view class="total-count align-center">
				<span class="title">数量</span>
				<span class="count">{{getSettlement.number}}</span>
			</view>
			<view class="total-price align-center">
				<view class="title">
					合计
				</view>
				<view class="price">
					<span class="unit">￥</span>
					<span class="price">{{getSettlement.total}}</span>
				</view>
			</view>
			<view class="order-button">
				<u-button text="下单" shape="circle" style="height: 75rpx;" color="#FA625D" :disabled="!isCar" @click="pushConfirmOrder">
				</u-button>
			</view>
		</view>
		<z-navigation></z-navigation>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				count: 0,
				list: [],
				getSettlement: null
			}
		},
		onShow() {
			this.getCart().then((res) => {
				this.getSettlement2()
			})
		},
		computed: {
			isCar() {
				return this.list.length?true:false
			}
		},
		methods: {
			popRefreshData() {
				this.getCart().then((res) => {
					this.getSettlement2()
				})
			},
			// 购物车列表
			async getCart() {
				const shopInfo = uni.getStorageSync('shopInfo');
				await this.$http.get("cart/getCart", {
					shopId: shopInfo.shopId
				}).then(res => {
					this.list = res
					let count = 0
					this.list.forEach((item, index) => {
						item.skus.forEach((skuItem, skuIndex) => {
							count = count + skuItem.number
						})
					})
					uni.$emit('cartCount', count)
				})
			},
			// 购物车总计
			getSettlement2() {
				const param = {
					shops: [
						// 	{
						// 	shopId: 187,
						// 	skus: [{
						// 		ifLogistics: 1,
						// 		number: 3,
						// 		selected: 1,
						// 		skuId: 1
						// 	}]
						// },
					]
				}
				this.list.forEach((item, index) => {
					param.shops.push({
						shopId: item.shopId,
						skus: []
					})
					item.skus.forEach((subItem, subIndex) => {
						param.shops[index].skus.push({
							ifLogistics: subItem.ifLogistics,
							number: subItem.number,
							selected: subItem.selected,
							skuId: subItem.skuId
						})
					})
				})
				this.$http.post("order/getSettlement2", param).then(res => {
					res.total = 0
					res.number = 0
					res.shops.forEach((item, index) => {
						item.skus.forEach((skuItem, skuIndex) => {
							res.total = res.total + skuItem.total
							res.number = res.number + skuItem.number
						})
					})
					res.total = res.total.toFixed(2)
					this.getSettlement = res
				})
			},
			pushConfirmOrder() {
				uni.navigateTo({
					url: '/pages/shopCar/confirmOrder'
				});
			},
			countChange(v, skuId) {
				console.log(v, skuId);
				if (v.value == 0) {
					this.cartDelete(skuId).then(res => {
						this.getCart().then(res => {
							this.getSettlement2()
						})
					})
					return
				}
				this.updateNumber(v.value, skuId).then(res => {
					this.getSettlement2()
				})
			},
			async cartDelete(skuId) {
				const param = {
					ids: [skuId]
				}
				await this.$http.post("cart/delete", param).then(res => {})
			},

			async updateNumber(number, skuId) {
				const param = {
					number: number,
					skuId: skuId
				}
				await this.$http.post("cart/updateNumber", param).then(res => {})
			},
			clear() {
				uni.showModal({
					title: '',
					content: '确定要清空所有商品吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post("cart/deleteAll").then(res => {
								this.getCart().then((res) => {
									this.getSettlement2()
								})
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});


			}

		}
	};
</script>

<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.shopbody {
		width: 750rpx;

		.gitem {
			width: 100%;
			height: 260rpx;
			padding: 20rpx;
			display: flex;
			background: #fff;

			.left {
				width: 205rpx;
				height: 100%;

				.img {
					width: 205rpx;
					height: 140rpx;
					box-shadow: 0px 2px 24px 0px rgba(51, 51, 51, 0.12);
					border-radius: 8rpx;
				}
			}

			.right {
				flex: 1;
				height: 100%;
				padding-left: 20rpx;

				.name {
					width: 436rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 600;
					color: #333333;
					line-height: 39rpx;
				}

				.spac {
					height: 60rpx;
				}

				.ops {
					.space-title {
						width: 70rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #666666;
						opacity: 0.6;
					}

					.space-content {
						width: 399rpx;
						height: 45rpx;
						line-height: 45rpx;
						font-size: 28rpx;
						background: #F6F7FC;
						border-radius: 4rpx;
						font-weight: 400;
						color: #333333;
						margin: 0 10rpx;
						padding: 0 14rpx;
					}
				}

				.price-count {
					margin-top: 20rpx;

					.price {
						.unit {
							font-size: 28rpx;
							font-family: SF UI Text;
							font-weight: bold;
							color: #E86C64;
							line-height: 48rpx;
							height: 28px;
						}

						.price {
							font-size: 36rpx;
							font-family: SF UI Text;
							font-weight: bold;
							color: #E86C64;
							line-height: 48rpx;
							height: 28px;
						}
					}

					.u-number-box,
					.u-number-box__minus,
					.u-number-box__plus {
						height: 58rpx !important;
					}

					.mt10 {}
				}
			}
		}
	}

	.total {
		position: fixed;
		bottom: 100rpx;
		height: 98rpx;
		line-height: 98rpx;
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0px 1rpx 8rpx 0px rgba(153, 153, 153, 0.1200);
		padding: 0 15rpx 0 40rpx;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
		z-index: 999;


		.total-count {
			.title {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.count {
				font-size: 40rpx;
				font-family: SF UI Text;
				font-weight: bold;
				color: #333333;
				padding: 0 22rpx 0 16rpx;
			}
		}

		.total-price {
			flex: 1;

			.title {
				width: 70rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.price {
				.unit {
					font-size: 28rpx;
					font-family: SF UI Text;
					font-weight: bold;
					color: #E86C64;
					height: 28px;
					padding: 0 0 0 15rpx;
				}

				.price {
					font-size: 36rpx;
					font-family: SF UI Text;
					font-weight: bold;
					color: #E86C64;
				}
			}
		}

		.order-button {
			height: 75rpx;
			width: 135rpx;
			float: right;

			/deep/.u-button {
				height: 75rpx !important;
			}
		}
	}
</style>
